<template>
  <div class="app-container">
    <div class="block">
        <el-row  :gutter="20">
          <el-col :span="6">
            <el-input v-model="listQuery.name" size="mini" placeholder="商品名称" clearable></el-input>
          </el-col>
          <el-col :span="6">
              <treeselect v-model="listQuery.categoryId"  size="mini"  :options="categories"  placeholder="请选择商品类别"/>
          </el-col>
          <el-col :span="6">
            <el-button type="success" size="mini" icon="el-icon-search" @click.native="search">{{ $t('button.search') }}</el-button>
            <el-button type="primary" size="mini" icon="el-icon-refresh" @click.native="reset">{{ $t('button.reset') }}</el-button>
          </el-col>
        </el-row>
      <el-row>
        <el-col :span="24">
          <el-button type="success" size="mini" icon="el-icon-download" @click.native="downloadExcel">导出</el-button>
          <el-popover
            placement="bottom-start"
            width="390"
            v-model="uploadFilePop"
            @hide="excelUploadHide"
          >
            <el-button
              slot="reference"
              class="filter-item"
              size="mini"
              type="danger"
              icon="el-icon-upload2"
            >导入</el-button>
            <el-upload
              ref="excelUpload"
              class="upload-demo"
              drag
              action="https://jsonplaceholder.typicode.com/posts/"
              :auto-upload="false"
              :limit = "1"
              :multiple="false"
              accept=".xls, .xlsx"
            >
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
              <div class="el-upload__tip" slot="tip">只能上传excel文件</div>
            </el-upload>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="uploadFilePop=false">取消</el-button>
              <el-button type="primary" :loading="uploadFileLoading" size="mini" @click="doUpload">确定</el-button>
            </div>
          </el-popover>



        </el-col>
      </el-row>
    </div>


    <el-table :data="list" v-loading="listLoading" element-loading-text="Loading" border fit highlight-current-row
              @current-change="handleCurrentChange">
      <el-table-column label="商品名称" width = "200" prop="name"></el-table-column>
      <el-table-column label="商品类别" width = "200" prop="categoryName"></el-table-column>
      <el-table-column label="商品规格" align="center" width = "200" prop="codeName"></el-table-column>
      <el-table-column label="库存" align="center" width = "100" prop="stock"></el-table-column>
      <el-table-column label="销售价格(元)" align="center" width = "150" prop="price"></el-table-column>
      <el-table-column label="活动名称" align="center" width = "150" prop="discountDesc"></el-table-column>
      <el-table-column label="活动价格(元)" align="center" width = "150" prop="discountPrice"></el-table-column>
      <el-table-column label="活动开始时间" align="center" width = "160" prop="discountPriceStart"></el-table-column>
      <el-table-column label="活动结束时间" align="center" width = "160" prop="discountPriceEnd"></el-table-column>
    </el-table>

    <el-pagination
      background
      layout="total, sizes, prev, pager, next, jumper"
      :page-sizes="[10,15, 20, 50, 100,500]"
      :page-size="listQuery.limit"
      :total="total"
      :current-page.sync="listQuery.page"
      @size-change="changeSize"
      @current-change="fetchPage"
      @prev-click="fetchPrev"
      @next-click="fetchNext">
    </el-pagination>

  </div>
</template>

<script >
import goodsApi from '@/api/shop/goods'
import {getApiUrl} from '@/utils/utils'
import permission from '@/directive/permission/index.js'
import categoryApi from "@/api/shop/category";
import orderApi from "@/api/shop/order";

export default {
  name:'goodsPriceList',
  directives: { permission },
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 10,
        name: '',
        categoryId: null
      },
      categories:[],
      total: 0,
      list: null,
      listLoading: true,
      selRow: {},
      apiUrl: getApiUrl(),
      uploadFilePop: false,
      uploadFileLoading: false
    }
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      categoryApi.getList().then(response => {
        this.categories = response.data
      })
      this.fetchData()
    },
    fetchData() {
      this.listLoading = true
      goodsApi.getGoodsPriceList(this.listQuery).then(response => {
        this.list = response.data.list
        this.listLoading = false
        this.total = response.data.total
      })
    },
    search() {
      this.listQuery.page = 1
      this.fetchData()
    },
    reset() {
      this.listQuery.name = ''
      this.listQuery.page = 1
      this.fetchData()
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    fetchNext() {
      this.listQuery.page = this.listQuery.page + 1
      this.fetchData()
    },
    fetchPrev() {
      this.listQuery.page = this.listQuery.page - 1
      this.fetchData()
    },
    fetchPage(page) {
      this.listQuery.page = page
      this.fetchData()
    },
    changeSize(limit) {
      this.listQuery.limit = limit
      this.fetchData()
    },
    handleCurrentChange(currentRow, oldCurrentRow) {
      this.selRow = currentRow
    },
    downloadExcel(){
      goodsApi.download(this.listQuery).then(response => {
        window.location.href= getApiUrl() + '/file/download?fileName='+response.data.realFileName
      })
    },
    excelUploadHide: function() {
      this.$refs.excelUpload.clearFiles()
    },
    doUpload: function() {
      var files = this.$refs.excelUpload.uploadFiles
      if (files.length < 1) {
        this.$message({
          message: '请上传文件!',
          type: 'error'
        })
        return
      }
      this.uploadFileLoading = true
      var params = {}
      params.file = files[0].raw
      goodsApi.excelUpload(params).then(res => {
        if (res.code === 20000) {
          this.$message({
            message: '上传成功',
            type: 'success',
            duration: 2500
          })
          this.fetchData()
        } else {
          // this.$alert('<strong>这是 <i>HTML</i> 片段</strong>', 'HTML 片段', {
          //   dangerouslyUseHTMLString: true
          // });
          this.$message({
            message: '上传失败',
            type: 'error',
            duration: 2500
          })
        }
        this.uploadFileLoading = false
        this.uploadFilePop = false
      }).catch(err => {
        this.uploadFileLoading = false
        this.uploadFilePop = false
      })
    },
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  @import "src/styles/common.scss";
</style>

